package com.ruoyi.baselibrary.infobasespecialist.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.baselibrary.infobasespecialist.domain.InfoBaseSpecialistFile;
import com.ruoyi.baselibrary.infobasespecialist.service.IInfoBaseSpecialistFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家附件Controller
 *
 * @author ruoyi
 * @date 2022-01-13
 */
@RestController
@RequestMapping("/infoBaseSpecialist/infoBaseSpecialist/file")
public class InfoBaseSpecialistFileController extends BaseController {
    @Autowired
    private IInfoBaseSpecialistFileService infoBaseSpecialistFileService;

    /**
     * 文件后缀 支持的类型 todo 自行舍取
     */
    private static final String[] FILE_SUFFIX_SUPPORT = {"xlsx", "xls", "doc", "docx","pdf","txt", "jpg", "jpeg", "png"};

    /**
     * 查询专家附件列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoBaseSpecialistFile infoBaseSpecialistFile) {
        List<InfoBaseSpecialistFile> list = infoBaseSpecialistFileService.selectInfoBaseSpecialistFileList(infoBaseSpecialistFile);
        return getDataTable(list);
    }

    /**
     * 导出专家附件列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:export')")
    @Log(title = "专家附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBaseSpecialistFile infoBaseSpecialistFile) {
        List<InfoBaseSpecialistFile> list = infoBaseSpecialistFileService.selectInfoBaseSpecialistFileList(infoBaseSpecialistFile);
        ExcelUtil<InfoBaseSpecialistFile> util = new ExcelUtil<InfoBaseSpecialistFile>(InfoBaseSpecialistFile.class);
        util.exportExcel(response, list, "专家附件数据");
    }

    /**
     * 获取专家附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBaseSpecialistFileService.selectInfoBaseSpecialistFileById(id));
    }

    /**
     * 新增专家附件
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:add')")
    @Log(title = "专家附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBaseSpecialistFile infoBaseSpecialistFile) {
        return toAjax(infoBaseSpecialistFileService.insertInfoBaseSpecialistFile(infoBaseSpecialistFile));
    }

    /**
     * 修改专家附件
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:edit')")
    @Log(title = "专家附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBaseSpecialistFile infoBaseSpecialistFile) {
        return toAjax(infoBaseSpecialistFileService.updateInfoBaseSpecialistFile(infoBaseSpecialistFile));
    }

    /**
     * 删除专家附件
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:remove')")
    @Log(title = "专家附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBaseSpecialistFileService.deleteInfoBaseSpecialistFileByIds(ids));
    }

    /**
     * 批量上传专家附件
     *
     * @param infoBaseSpecialistFiles
     * @return
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:file:add')")
    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    public AjaxResult batchAdd(@RequestBody ArrayList<InfoBaseSpecialistFile> infoBaseSpecialistFiles) {
        int i = 0;
        for (InfoBaseSpecialistFile infoBaseSpecialistFile : infoBaseSpecialistFiles) {
            String[] fileSuffix=infoBaseSpecialistFile.getFileName().split("\\.");
            if(Arrays.asList(FILE_SUFFIX_SUPPORT).contains(fileSuffix[fileSuffix.length-1].toLowerCase(Locale.ROOT))){
                i = infoBaseSpecialistFileService.insertInfoBaseSpecialistFile(infoBaseSpecialistFile);
            }else{
                return  AjaxResult.error("请上传符合要求的后缀文件");
            }

        }
        return toAjax(i);
    }
}
